home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / FWDPARSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  13.6 KB  |  607 lines

  1. /* #define TEST */
  2. #ifdef TEST
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #else
  7. #include "global.h"
  8. #include "commands.h"
  9. #endif
  10. #ifndef MSDOS
  11. #include <time.h>
  12. #endif
  13.  
  14. #if !defined(_lint)
  15. static char rcsid[] OPTIONAL = "$Id: fwdparse.c,v 1.18 1997/08/19 01:19:22 root Exp root $";
  16. #endif
  17.  
  18.  
  19. #define OUTSIDE_BBS    0
  20. #define INSIDE_BBS    1
  21. #define FWD_SCRIPT    2
  22. #define FWD_AREAS    3
  23.  
  24.  
  25. extern int FWDmode, FWDminidle, FWDlimittime;
  26.  
  27. #ifdef TEST
  28. #include "fwdparse.h"
  29. #define Forwardfile "new.bbs"
  30. #define NULLFILE ((FILE *)0)
  31. #define NULLCHAR ((char *)0)
  32. #define stricmp strcasecmp
  33. #define strnicmp strncasecmp
  34. #define NUMMBX    40
  35. #define MBFWD 1
  36. #define READ_TEXT "rt"
  37. int FWDmode = 1;
  38. int FWDminidle = 30;
  39. int FWDlimittime = 0;
  40. char *skipnonwhite (char *cp);
  41. char *skipwhite (char *cp);
  42. void rip (register char *s);
  43. char *strlwr(char *s);
  44. #else
  45. #include "files.h"
  46. #include "mailbox.h"
  47. #undef connect
  48. #endif
  49.  
  50.  
  51. static const char *tokens[] = {
  52.     "alttime", "alwaysupdate", "areas", "bulletins", "clockoffset",
  53.     "clockset", "connect", "dumbpms", "elif", "else", "endif",
  54.     "fbbsize", "if", "limittime", "limittype", "minidle", "personals",
  55.     "poll",    "revincoming", "script", "size", "subchannel", "tries"
  56. };
  57.  
  58.  
  59. #define TOKEN_ALTTIME        0
  60. #define TOKEN_ALWAYSUPD        1
  61. #define TOKEN_AREAS        2
  62. #define TOKEN_BULLETINS        3
  63. #define TOKEN_CLOCKOFFSET    4
  64. #define TOKEN_CLOCKSET        5
  65. #define TOKEN_CONNECT        6
  66. #define TOKEN_DUMBPMS        7
  67. #define TOKEN_ELIF        8
  68. #define TOKEN_ELSE        9
  69. #define TOKEN_ENDIF        10
  70. #define TOKEN_FBBSIZE        11
  71. #define TOKEN_IF        12
  72. #define TOKEN_LIMITTIME        13
  73. #define TOKEN_LIMITTYPE        14
  74. #define TOKEN_MINIDLE        15
  75. #define TOKEN_PERSONALS        16
  76. #define TOKEN_POLL        17
  77. #define TOKEN_REVINCOMING    18
  78. #define TOKEN_SCRIPT        19
  79. #define TOKEN_SIZE        20
  80. #define TOKEN_SUBCHANNEL    21
  81. #define TOKEN_TRIES        22
  82. #define MAXTOKENS        23
  83.  
  84.  
  85. #define IFTRUE            1
  86. #define IFFALSE            0
  87. #define IFFOUND            2
  88. #define IFINVALID        -1
  89.  
  90.  
  91.  
  92. #ifdef MBFWD
  93. /* get any groups of four digits that specify the begin and ending hours of
  94.  * forwarding. Returns 1 if forwarding may take place.
  95.  */
  96. static int
  97. timeok(char *line)
  98. {
  99. time_t now;
  100. int h1, h2, m1, m2;
  101. struct tm *t;
  102.  
  103.     if (strlen (line) < 9)
  104.         return 0;
  105.  
  106.     h1 = (*line++ - '0') * 10;
  107.     h1 += (*line++ - '0');
  108.     m1 = (*line++ - '0') * 10;
  109.     m1 += (*line++ - '0');
  110.     if (*line++ != '-')
  111.         return 0;
  112.     h2 = (*line++ - '0') * 10;
  113.     h2 += (*line++ - '0');
  114.     m2 = (*line++ - '0') * 10;
  115.     m2 += (*line++ - '0');
  116.  
  117.     (void) time(&now);
  118.     t = localtime (&now);
  119.     if (h1 > t->tm_hour || h2 < t->tm_hour)
  120.         return 0;
  121.     if (h1 == t->tm_hour && m1 > t->tm_min)
  122.         return 0;
  123.     if (h2 == t->tm_hour && m2 < t->tm_min)
  124.         return 0;
  125.  
  126.     return 1;    /* not too early or too late */
  127. }
  128.  
  129.  
  130.  
  131. static int
  132. dayok(char *line)
  133. {
  134. struct tm *t;
  135. time_t now;
  136. char *cp;
  137. int startday, endday;
  138.  
  139.     cp = strchr (line, '-');
  140.     if (cp)    {
  141.         *cp++ = 0;
  142.         endday = (atoi (cp) % 7);
  143.     } else
  144.         endday = (atoi (line) % 7);
  145.  
  146.     startday = (atoi (line) % 7);
  147.     
  148.     now = time((time_t *)0);
  149.     t = localtime (&now);
  150.     if (t->tm_wday < startday || t->tm_wday > endday)
  151.         return 0;
  152.     else
  153.         return 1;
  154. }
  155.  
  156.  
  157.                                     
  158. static int
  159. gettoken (char *str)
  160. {
  161. int k;
  162.  
  163.     for (k = 0; k < MAXTOKENS; k++)
  164.         if (!strnicmp (str, tokens[k], strlen(str)))
  165.             break;
  166.     return k;
  167. }
  168.  
  169.  
  170.  
  171. struct fwdbbs *
  172. fwdread (char *name OPTIONAL, int try OPTIONAL)
  173. {
  174. struct fwdbbs *f;
  175. FILE *fp, *fpsave = NULLFILE;
  176. char buffer[81], *cp, *tp, *next;
  177. int thestate = OUTSIDE_BBS;
  178. struct fscript *s, *lasts = NULLFSCRIPT;
  179. struct arealist *a, *lasta = NULLAREALIST;
  180. struct altlist *alt, *lastalt = NULLALTLIST;
  181. int token, iflevel = 0;
  182. char ifcondition[IFLEVELS + 1];
  183. int cnt = 0;
  184.  
  185.     if ((fp = fopen (Forwardfile, "r")) == NULLFILE)
  186.         return NULLFWDBBS;
  187.  
  188.     ifcondition[iflevel] = IFINVALID;
  189.     f = callocw (1, sizeof (struct fwdbbs));
  190.     if (f == NULLFWDBBS)
  191.         return f;
  192.  
  193.     while (fgets (buffer, 80, fp))    {
  194.         if (*buffer != '[')
  195.             continue;
  196.         if ((cp = strchr (buffer, ']')) == NULLCHAR)
  197.             continue;
  198.         *cp = 0;
  199.         if ((!name && cnt++ == try) || (name && !stricmp (&buffer[1], name)))    {
  200.             f->name = strdup (&buffer[1]);
  201.             (void) strlwr (f->name);
  202.             try = 1;
  203.             break;
  204.         }
  205.     }
  206.  
  207.     if (!f->name)    {
  208.         (void) fclose (fp);
  209.         free (f);
  210.         return NULLFWDBBS;
  211.     }
  212.     thestate = INSIDE_BBS;
  213.     f->ALTtime = 720;
  214.     f->subchannel = -1;
  215.     f->minidle = FWDminidle;
  216.     f->limittime = FWDlimittime;
  217.     f->maxtries = 1;
  218.     f->subchannel = (char) NOSUBCHANNEL;
  219.     f->personals = f->bulletins = 1;
  220.     f->limittype = FWDTYPE_FBB;
  221.  
  222.     while (1)    {            /*lint !e716 !e774 */
  223.         if (fgets (buffer, 80, fp) == NULLCHAR)    {
  224.             if (fpsave)    {
  225.                 (void) fclose (fp);
  226.                 fp = fpsave;
  227.                 fpsave = NULLFILE;
  228.                 continue;
  229.             }
  230.             break;
  231.         }
  232.         rip (buffer);
  233.         cp = skipwhite (buffer);
  234.         if (*cp == '-' || *cp == '[')
  235.             break;
  236.         if (!*cp || *cp == '#' || *cp == ';')
  237.             continue;
  238.         if (*cp == '}')    {
  239.             thestate = INSIDE_BBS;
  240.             continue;
  241.         }
  242.         if (!strnicmp (cp, "include", 7)) {
  243.             cp = skipwhite (&cp[7]);
  244.             fpsave = fp;
  245.             if((fp = fopen (cp, READ_TEXT)) == NULLFILE)    {
  246.                 fp = fpsave;
  247.                 fpsave = NULLFILE;
  248.             }
  249.             continue;
  250.         }
  251.  
  252.         switch (thestate)    {
  253.             case INSIDE_BBS:
  254.                 if ((tp = strchr (cp, '=')) != NULLCHAR)    {
  255.                     *tp = 0;
  256.                     next = skipwhite (tp + 1);
  257.                     while (*(tp - 1) == ' ')
  258.                         *--tp = 0;
  259.                 } else
  260.                     next = &cp[strlen(cp)];
  261.                 if ((tp = strchr (cp, ' ')) != NULLCHAR)
  262.                     *tp++ = 0;
  263.                 token = gettoken(cp);
  264.                 if ((ifcondition[iflevel] == IFFOUND) && token != TOKEN_ENDIF)
  265.                     continue;
  266.                 if ((ifcondition[iflevel] == IFFALSE) && token != TOKEN_ELSE && token != TOKEN_ELIF && token != TOKEN_ENDIF)
  267.                     continue;
  268.                 switch (token)    {
  269.                     case TOKEN_ELIF:    if (!iflevel)
  270.                                     break;
  271.                                 if (ifcondition[iflevel] == IFTRUE)    {
  272.                                     ifcondition[iflevel] = IFFOUND;
  273.                                     break;
  274.                                 }
  275.                                 iflevel--;
  276.                                 /* and fall through */
  277.                     case TOKEN_IF:        if (iflevel < IFLEVELS)        {
  278.                                     /* set ifcondition to 1 if true, 0 if false */
  279.                                     if (!strnicmp (tp, "try", 3))
  280.                                         ifcondition[++iflevel] = (try == atoi (next));
  281.                                     else if (!strnicmp (tp, "time", 4))
  282.                                         ifcondition[++iflevel] = (char) timeok (next);
  283.                                     else if (!strnicmp (tp, "mode", 4))
  284.                                         ifcondition[++iflevel] = (FWDmode == atoi (next));
  285.                                     else if (!strnicmp (tp, "dayofweek", 9))
  286.                                         ifcondition[++iflevel] = (char) dayok (next);
  287.                                     else if (token == TOKEN_ELIF)
  288.                                         iflevel++;
  289.                                 }
  290.                                 break;
  291.                     case TOKEN_ELSE:    if (iflevel && ifcondition[iflevel] != IFFOUND)
  292.                                     ifcondition[iflevel] = (ifcondition[iflevel] == IFTRUE) ? IFFOUND : IFTRUE;
  293.                                 break;
  294.                     case TOKEN_ENDIF:    if (iflevel)
  295.                                     ifcondition[iflevel--] = IFINVALID;
  296.                                 break;
  297.                     case TOKEN_ALTTIME:    f->ALTtime = atol (next);
  298.                                 break;
  299.                     case TOKEN_LIMITTIME:    f->limittime = (time_t) (atoi (next) * 60);
  300.                                 break;
  301.                     case TOKEN_AREAS:    thestate = FWD_AREAS;
  302.                                 break;
  303.                     case TOKEN_SCRIPT:    thestate = FWD_SCRIPT;
  304.                                 break;
  305.                     case TOKEN_CLOCKSET:    f->noclockset = !stricmp (next, "no");
  306.                                 break;
  307.                     case TOKEN_ALWAYSUPD:    f->alwaysupdate = !stricmp (next, "yes");
  308.                                 break;
  309.                     case TOKEN_CLOCKOFFSET:    f->clockoffset = (char) atoi (next);
  310.                                 break;
  311.                     case TOKEN_SIZE:    f->maxsize = atol (next);
  312.                                 break;
  313.                     case TOKEN_MINIDLE:    f->minidle = atoi (next);
  314.                                 break;
  315.                     case TOKEN_FBBSIZE:    f->fbbsize = atol (next);
  316.                                 break;
  317.                     case TOKEN_LIMITTYPE:    if (!strnicmp (next, "FBB-NON", 7))
  318.                                     f->limittype = FWDTYPE_FBBNON;
  319.                                 else if (!strnicmp (next, "MBL", 3) || !strnicmp (next, "RLI", 3))
  320.                                     f->limittype = FWDTYPE_MBLRLI;
  321.                                 else if (!strnicmp (next, "FBB", 3))
  322.                                     f->limittype = FWDTYPE_FBB;
  323.                                 else if (!strnicmp (next, "X-", 2))
  324.                                     f->limittype = FWDTYPE_XFWD;
  325.                                 break;
  326.                     case TOKEN_TRIES:    f->maxtries = (short) atol (next);
  327.                                 break;
  328.                     case TOKEN_SUBCHANNEL:    f->subchannel = (char) atoi (next);
  329.                                 break;
  330.                     case TOKEN_REVINCOMING:    f->noreverseincoming = !stricmp (next, "no");
  331.                                 break;
  332.                     case TOKEN_PERSONALS:    f->personals = !stricmp (next, "yes");
  333.                                 break;
  334.                     case TOKEN_DUMBPMS:    f->dumbpms = !stricmp (next, "yes");
  335.                                 break;
  336.                     case TOKEN_BULLETINS:    f->bulletins = !stricmp (next, "yes");
  337.                                 break;
  338.                     case TOKEN_POLL:    f->poll = !stricmp (next, "yes");
  339.                                 break;
  340.                     case TOKEN_CONNECT:    f->conn = strdup (next);
  341.                                 break;
  342.                     default:        break;
  343.                 }
  344.                 break;
  345.             case FWD_SCRIPT:
  346.                 s = callocw (1, sizeof(struct fscript));
  347.                 if (s != NULLFSCRIPT)    {
  348.                     if (!strnicmp(cp, "wait", 4))    {
  349.                         s->linetype = SCRIPT_WAIT;
  350.                         tp = skipnonwhite (cp);
  351.                         tp = skipwhite (tp);
  352.                         s->timeout = (short) atoi (tp);
  353.                         tp = skipnonwhite (tp);
  354.                         tp = skipwhite (tp);
  355.                         s->string = strdup (tp);
  356.                     } else if (!strnicmp (cp, "send", 4) || !strnicmp (cp, "sid", 3))    {
  357.                         tp = skipnonwhite (cp);
  358.                         tp = skipwhite (tp);
  359.                         s->string = strdup (tp);
  360.                         if (!strnicmp(cp, "sid", 3))
  361.                             s->linetype = SCRIPT_SID;
  362.                         else
  363.                             s->linetype = SCRIPT_SEND;
  364.                     } else    {
  365.                         free (s);
  366.                         break;
  367.                     }
  368.                     if (lasts)
  369.                         lasts->next = s;
  370.                     else
  371.                         f->script = s;
  372.                     lasts = s;
  373.                 }
  374.                 break;
  375.             case FWD_AREAS:
  376.                 a = callocw (1, sizeof(struct arealist));
  377.                 if (a != NULLAREALIST)    {
  378.                     tp = skipnonwhite (cp);
  379.                     *tp++ = 0;
  380.                     a->name = strdup (cp);
  381.                     tp = skipwhite (tp);
  382.                     if (*tp && strnicmp (tp, "ALT", 3))    {
  383.                         cp = tp;
  384.                         tp = skipnonwhite (tp);
  385.                         *tp++ = 0;
  386.                         a->forceaddr = strdup (cp);
  387.                     }
  388.                     lastalt = NULLALTLIST;
  389.                     while ((tp = skipwhite (tp)) != NULLCHAR)    {
  390.                         if (strnicmp (tp, "ALT", 3))
  391.                             break;
  392.                         if (tp[3] != '=')    {
  393.                             a->isALT = 1;
  394.                             break;
  395.                         }
  396.                         alt = callocw (1, sizeof(struct altlist));
  397.                         if (alt != NULLALTLIST)    {
  398.                             alt->minutes = f->ALTtime;
  399.                             cp = &tp[4];
  400.                             tp = strpbrk (cp, ", \t");
  401.                             if (tp)    {
  402.                                 next = tp;
  403.                                 if (*tp++ == ',')    {
  404.                                     alt->minutes = atoi(tp);
  405.                                     tp = skipnonwhite(tp);
  406.                                 }
  407.                                 *next = 0;
  408.                             } else
  409.                                 tp = &cp[strlen(cp)];
  410.                             alt->name = strdup (cp);
  411.                             if (lastalt)
  412.                                 lastalt->next = alt;
  413.                             else
  414.                                 a->alternates = alt;
  415.                             lastalt = alt;
  416.                         }
  417.                     }
  418.                     if (lasta)
  419.                         lasta->next = a;
  420.                     else
  421.                         f->areas = a;
  422.                     lasta = a;
  423.                 }
  424.                 break;
  425.             default:
  426.                 break;
  427.         }
  428.     }
  429.  
  430.     (void) fclose (fp);
  431.     return f;
  432. }
  433.  
  434. #else
  435.  
  436.  
  437.  
  438. struct fwdbbs *
  439. fwdread (char *name OPTIONAL, int try OPTIONAL)
  440. {
  441.     return NULLFWDBBS;
  442. }
  443. #endif
  444.  
  445.  
  446.  
  447. void
  448. fwdfree (struct fwdbbs **f)
  449. {
  450. struct fscript *s, *stmp;
  451. struct arealist *a, *atmp;
  452. struct altlist *alt, *alttmp;
  453.  
  454.     if (*f == NULLFWDBBS)
  455.         return;
  456.     free ((*f)->name);
  457.     free ((*f)->conn);
  458.     s = (*f)->script;
  459.     while (s)    {
  460.         free (s->string);
  461.         stmp = s;
  462.         s = s->next;
  463.         free (stmp);
  464.     }
  465.     a = (*f)->areas;
  466.     while (a)    {
  467.         free (a->name);
  468.         free (a->forceaddr);
  469.         alt = a->alternates;
  470.         while (alt)    {
  471.             free (alt->name);
  472.             alttmp = alt;
  473.             alt = alt->next;
  474.             free (alttmp);
  475.         }
  476.         atmp = a;
  477.         a = a->next;
  478.         free (atmp);
  479.     }
  480.     free (*f);
  481.     *f = NULLFWDBBS;
  482. }
  483.  
  484.  
  485. #ifdef TEST
  486. char *
  487. strlwr(s)
  488. char *s;
  489. {
  490. register char *p = s;
  491.  
  492.     while (*p)
  493.         *p = (char) tolower (*p), p++;
  494.     return s;
  495. }
  496.  
  497.  
  498. char *
  499. skipnonwhite (cp)
  500. char *cp;
  501. {
  502.         while (*cp && *cp != ' ' && *cp != '\t')
  503.                 cp++;
  504.         return (cp);
  505. }
  506.  
  507.  
  508. char *
  509. skipwhite (cp)
  510. char *cp;
  511. {
  512.         while (*cp && (*cp == ' ' || *cp == '\t'))
  513.                 cp++;
  514.         return (cp);
  515. }
  516.  
  517.  
  518.  
  519. void
  520. rip(s)
  521. register char *s;
  522. {
  523.         register char *cp;
  524.  
  525.         if((cp = strchr(s,'\n')) != NULLCHAR)
  526.                 *cp = '\0';
  527. }
  528.  
  529.  
  530. int
  531. main (int argc, char *argv[])
  532. {
  533. struct fwdbbs *f;
  534. struct fscript *s;
  535. struct arealist *a;
  536. struct altlist *alt;
  537. int try = 1;
  538.  
  539.     if (argc < 2)    {
  540.         printf ("No BBS name given\n");
  541.         exit (1);
  542.     }
  543.     if (argc > 2)    {
  544.         try = atoi (argv[2]);
  545.         if (!try)
  546.             try = 1;
  547.     }
  548.     f = fwdread (argv[1], try);
  549.     if (f == NULLFWDBBS)    {
  550.         printf ("BBS '%s' not found\n", argv[1]);
  551.         exit (1);
  552.     }
  553.     printf ("[%s]\nCONNECT = %s\nALTTIME = %ld\nSIZE = %ld\nPOLL = %s\n",
  554.         f->name, f->conn, f->ALTtime, f->maxsize, (f->poll) ? "YES" : "NO");
  555.     printf ("SUBCHANNEL = %d\nREVINCOMING = %s\nFBBSIZE = %ld\n",
  556.         f->subchannel, (f->noreverseincoming) ? "NO" : "YES",
  557.         f->fbbsize);
  558.     printf ("PERSONALS = %s\nBULLETINS = %s\n", (f->personals) ? "YES" : "NO",
  559.         (f->bulletins) ? "YES" : "NO");
  560.     printf ("CLOCKSET = %s\nCLOCKOFFSET = %d\nTRIES = %d\n",
  561.         (f->noclockset) ? "NO" : "YES", f->clockoffset, f->maxtries);
  562.     s = f->script;
  563.     if (s)    {
  564.         printf ("\nSCRIPT = {\n");
  565.         while (s)    {
  566.             switch (s->linetype)    {
  567.                 case SCRIPT_SEND:    printf (" SEND %s\n", s->string);
  568.                             break;
  569.                 case SCRIPT_WAIT:    printf (" WAIT %d %s\n", s->timeout, s->string);
  570.                             break;
  571.                 case SCRIPT_SID:    printf (" SID %s\n", s->string);
  572.             }
  573.             s = s->next;
  574.         }
  575.         printf ("}\n\n");
  576.     }
  577.     a = f->areas;
  578.     if (a)    {
  579.         printf ("\nAREAS = {\n");
  580.         while (a)    {
  581.             printf (" %s", a->name);
  582.             if (a->forceaddr)
  583.                 printf (" %s", a->forceaddr);
  584.             if (a->isALT)
  585.                 printf (" ALT\n");
  586.             else {
  587.                 alt = a->alternates;
  588.                 if (alt)    {
  589.                     while (alt)    {
  590.                         printf (" ALT=%s", alt->name);
  591.                         if (alt->minutes != f->ALTtime)
  592.                             printf (",%ld", alt->minutes);
  593.                         alt = alt->next;
  594.                     }
  595.                 }
  596.                 putchar ('\n');
  597.             }
  598.             a = a->next;
  599.         }
  600.         printf ("}\n\n");
  601.     }
  602.     fwdfree (&f);
  603.     return 0;
  604. }
  605.  
  606. #endif
  607.